/**
 * Created by CodeTlan.
 * User: Ing. Miguel Salas
 * Date: 3/11/12
 * Time: 4:28 PM
 */
Ext.define('CT.controller.Users', {
    extend: 'Ext.app.Controller',
    views:[
        'usuario.Window',
        'usuario.Grid',
        'usuario.Form',
        'usuario.WinPassword',
        'usuario.FormPassword'
    ],
    stores:[
        'Users'
    ],
    models:[
        'User'
    ],
    requires:[
        'Ctc.view.ComboPaging',
        'CT.view.rol.Combo'
    ],
    win:false,
    init:function(){
        this.control({
            'formuser button[action=add]': {
                click: this.agregar
            },
            'griduser button[action=chgPassword]': {
                click: this.btnWinPassword
            },
            'griduser':{
                edit:this.editar,
                beforeedit:this.setCombo,
                select:this.btnPasswordEnable
            },
            'griduser checkcolumn':{
                checkchange:this.activar
            },
            'formpassword button[action=addpassword]': {
                click: this.cambiarPassword
            }
        });
    },
    lanzarVentana:function(anim,icon){
        if(!this.win){
            this.init();
            this.win=Ext.widget('winusers',{animateTarget:anim,iconCls:icon});
        }
        this.win.show();
    },
    btnPasswordEnable:function(model){
        model.view.panel.down('#btnPassword').enable();
    },
    agregar:function(button){
        var form = button.up('form').getForm();
        var store=this.getUsersStore();
        if (form.isValid()) {
            form.submit({
                url:'index.php/users/add',
                success: function() {
                    form.reset();
                    Notification.ok('Usuario agregado',' ');
                    store.load();
                }
            });
        }
    },

    editar:function(editor, e){

        if(e.colIdx==4){
            var store = e.column.getEditor().store;
            var ind = store.findExact('titulo',e.value);
            var rol_k = store.getAt(ind).data.rol_k;
            var mods='{"rol_k" : "'+rol_k+'"}';
        }
        else{
            var mods='{"'+e.field+'":"'+e.value+'"}';
        }
        Ext.Ajax.request({
            url: 'index.php/users/edit',
            params: {
                mod: mods,
                usuario_k:e.record.data.usuario_k
            },
            success: function(response){
                e.record.commit();
                Notification.ok('Usuario Modificado',' ');
            }
        });
    },

    activar:function(check, index, activo){
        var store = this.getUsersStore();
        var record = store.getAt(index);
        Ext.Ajax.request({
            url: 'index.php/users/active',
            params: {
                usuario_k: record.data.usuario_k,
                activo: activo?1:0
            },
            success: function(response){
                record.commit();
                Notification.ok('Usuario Modificado',' ');
            }
        });
    },
    setCombo:function(editor, e){


        setTimeout(function(){
            if(editor.colIdx==4){
                var field=editor.column.getEditor();
                field.setRawValue(editor.record.data.rol);

                if(Ext.isEmpty(field.store.getTotalCount()))
                    field.store.load();
            }
        },100);
    },
    btnWinPassword:function(button){
        var record=button.up("grid").getSelectionModel().getSelection();
        this.usuario_k=record[0].data.usuario_k;
        var win=Ext.widget('winpassword',{iconCls:'key'});
        win.show();
    },
    cambiarPassword:function(button){
        var form = button.up('form').getForm();
        var win = button.up('window');

        if (form.isValid()) {
            form.submit({
                url:'index.php/users/pass',
                params:{
                    usuario_k:this.usuario_k
                },
                success: function() {
                    Notification.ok('Password Modificado',' ');
                    win.destroy();
                }
            });
        }
    }
});